<template>
  <div>
    <a-button class="mb-2 float-right" :disabled="selectedRowKeys.length === 0" type="primary" :loading="btnLoading" @click="onReceiptAll()">
      批量签收
    </a-button>
    <BaseTable
      fill
      row-key="id"
      :scroll="{ x: 1900 }"
      column-code="00001122"
      :loading="!!store.loading"
      :data-source="store.unSignData"
      :row-selection="{ selectedRowKeys, onChange: onSelectChange }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'signNurseStatusName'">
          <a-tag v-if="record.signNurseStatusName" color="blue">{{ record.signNurseStatusName }}</a-tag>
        </template>
        <template v-if="column.dataIndex === 'advExecTypeName'">
          {{ record.advExecTypeName }}
        </template>
        <template v-if="column.dataIndex === 'children'">
          <template v-if="record.advOrderType === 0">
            <DoctorAdviceOrder
              :row-data="record"
              :params-key="{
                mc: 'adv.mc',
                gg: 'adv.gg',
                sl: 'clinicalDose',
                dw: 'clinicalDoseUnit',
                yf: 'deliverRoute.fyff',
                pl: 'fqy.plid',
                drugPathName: 'drugPathName'
              }"
            />
          </template>
          <template v-else>
            <a-tooltip>
              <template #title>{{ record?.advName }}</template>
              {{ record?.advName }}
            </a-tooltip>
          </template>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <BaseButton confirm-text="确定签收吗？" type="link" size="small" @click="onReceipt([record.id])"> 签收 </BaseButton>
        </template>
      </template>
    </BaseTable>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import useStore from '../store'

const store = useStore()
const btnLoading = ref(false)
const selectedRowKeys = ref([])

watch(
  () => store.unSignData,
  () => {
    selectedRowKeys.value = []
  },
  { immediate: true }
)
const onSelectChange = (keys) => {
  selectedRowKeys.value = keys
}
const onReceipt = async (yzbhs) => {
  const res = await post('/inp/clinicalOrder/doCopyClinicalOrder', {
    yzbhs: yzbhs
  })
  btnLoading.value = false
  res.code === 0 ? message.success(res.msg) : message.error(res.msg || '操作失败')
  store.fetchSignData()
  store.fetchSignCountData()
  store.fetchAllOrderCountData()
}

const onReceiptAll = () => {
  btnLoading.value = true
  onReceipt(selectedRowKeys.value.filter((item) => !!item))
}
</script>
